package org.september.pisces.led.service;

import org.september.pisces.led.entity.Led;
import org.september.pisces.led.entity.LedArea;
import org.september.pisces.led.entity.LedFlash;
import org.september.pisces.led.entity.LedPlan;
import org.september.pisces.led.service.shizhan.MyUdpSocket;
import org.september.pisces.led.service.shizhan.ShiZhanLEDManager;
import lombok.extern.slf4j.Slf4j;
import org.september.smartdao.CommonDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

import static org.september.pisces.led.service.LedMemory.*;


@Slf4j
@Service
public class LedShowService {

    @Autowired
    private CommonDao dao;


    private final ExecutorService executorService = Executors.newFixedThreadPool(4);




    //启动时把库里加载到内存
    public void init() {
        if (!ledMap.isEmpty()){
            for (Long id : ledMap.keySet()) {
                oneLedClose(id);
            }
            ledMap.clear();
        }
        planList = dao.listByExample(new LedPlan());
        flashList = dao.listByExample(new LedFlash());
        List<LedArea> areas = dao.listByExample(new LedArea());
        for (LedFlash flash : flashList) {
            List<LedArea> list = areas.stream().filter(x -> Objects.equals(x.getFlashId(), flash.getId())).collect(Collectors.toList());
            flash.setAreas(list);
        }
        Map<Long, LedFlash> map = flashList.stream().collect(Collectors.toMap(LedFlash::getId, x -> x));
        for (LedPlan plan : planList) {
            String flashIds = plan.getFlashIds();
            if (StringUtils.hasText(flashIds)) {
                List<LedFlash> flashes1 = plan.getFlashes();
                String[] split = flashIds.split(",");
                for (String s : split) {
                    long id = Long.parseLong(s);
                    LedFlash ledFlash = map.get(id);
                    flashes1.add(ledFlash);
                }
                String collect = flashes1.stream().map(LedFlash::getFlashName).collect(Collectors.joining(","));
                plan.setFlashString(collect);
            }
        }
        ledList = dao.listByExample(new Led());
        Map<Long, LedPlan> planMap = planList.stream().collect(Collectors.toMap(LedPlan::getId, x -> x));
        for (Led led : ledList) {
            ShiZhanLEDManager manager = new ShiZhanLEDManager(new MyUdpSocket(),  led.getHost(), led.getPort());//6666
            led.setManager(manager);
            Long planId = led.getPlanId();
            if (planId!=null&&planMap.containsKey(planId)){
                LedPlan plan = planMap.get(planId);
                planMap.put(planId,plan);
                led.setPlan(plan);
                led.setPlanName(plan.getPlanName());
            }
            if (led.getEnableFlag() == 1) {
                oneLedStart(led);
            }
        }
    }

    public void oneLedStart(Led led) {
        if (led.getPlan() == null) {
            log.warn("未配置方案,无法发布:{}", led.getHost());
            return;
        }

        LedThread thread = new LedThread(led);
        ledMap.put(led.getId(), thread);
        executorService.execute(thread);
    }

    public void oneLedClose(Long id) {
        if (!ledMap.containsKey(id)) {
            log.warn("不存在线程,无需关闭:{}", id);
            return;
        }
        LedThread ledThread = ledMap.get(id);
        ledThread.setChangeFlag(true); //停掉线程
        ledMap.remove(id);
    }
}
